package com.est.android.temperature;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class Convert extends Activity {
	private EditText text;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        text = (EditText) findViewById(R.id.EditText01);
    }
    
    public void myClickHandler(View view){
    	switch(view.getId()) {
    	case R.id.Button01:
    		RadioButton celsiusButton = (RadioButton)findViewById(R.id.RadioButton01);
    		RadioButton fahrButton = (RadioButton)findViewById(R.id.RadioButton02);
    		
    		if(text.getText().length() == 0) {
    			Toast.makeText(this, "Please enter a valid number", Toast.LENGTH_LONG).show();
    			
    			return;
    		}
    		
    		float inputValue = Float.parseFloat(text.getText().toString());
    		if(celsiusButton.isChecked()) {
    			text.setText(String.valueOf(convertFahrToCelsius(inputValue)));
    		}
    		else {
    			text.setText(String.valueOf(convertCelsiusToFahr(inputValue)));
    		}
    		
    		if(fahrButton.isChecked()){
    			fahrButton.setChecked(false);
    			celsiusButton.setChecked(true);
    		}
    		else {
    			fahrButton.setChecked(true);
    			celsiusButton.setChecked(false);
    		}
    	}
    }
    
    private float convertFahrToCelsius(float fahr) {
    	return ((fahr - 32) * 5/9);
    }
    
    private float convertCelsiusToFahr(float celsius) {
    	return ((celsius * 9) / 5) + 32;
    }
}